package org.abratuhi.kvngroupware.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;
import javax.swing.border.TitledBorder;

import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.controller.ChatroomController;
import org.abratuhi.kvngroupware.model.Chatroom;
import org.abratuhi.kvngroupware.spring.SpringUtilities;

@SuppressWarnings("serial")
public class GUIChatroom extends JPanel{
	public JList listUsers = new JList(new String[0]);
	public JTextArea textareaMessages = new JTextArea();
	public JTextArea textareaMessage = new JTextArea();
	public JButton buttonSend = new JButton();
	
	KVNGroupwareClient kvnclient;
	Chatroom kvnchatroom;
	ChatroomController controller;
	
	JFrame frame;
	
	public GUIChatroom(KVNGroupwareClient kvnclient, Chatroom chatroom, Properties locale){
		super();
		
		this.kvnclient = kvnclient;
		this.kvnchatroom = chatroom;
		this.controller = new ChatroomController(this.kvnclient, this, this.kvnchatroom);
		
		this.buttonSend.setText(locale.getProperty("chatroom.button.message.send"));
		
		listUsers.setPreferredSize(new Dimension(100, 200));

		textareaMessages.setRows(40);
		textareaMessages.setColumns(40);
		textareaMessages.setWrapStyleWord(true);
		textareaMessages.setLineWrap(true);

		textareaMessage.setRows(3);
		textareaMessage.setColumns(40);
		textareaMessage.setWrapStyleWord(true);
		textareaMessage.setLineWrap(true);
		
		buttonSend.addActionListener(controller);

		JPanel users = new JPanel(new SpringLayout());
		users.setBorder(new TitledBorder(locale.getProperty("chatroom.border.users")));
		users.add(new JScrollPane(listUsers));
		SpringUtilities.makeCompactGrid(users, 1, 1, 0, 0, 0, 0);

		JPanel send = new JPanel(new SpringLayout());
		send.add(new JScrollPane(textareaMessage));
		send.add(buttonSend);
		SpringUtilities.makeCompactGrid(send, 1, 2, 0, 0, 0, 0);

		JPanel chat = new JPanel(new SpringLayout());
		chat.setBorder(new TitledBorder(locale.getProperty("chatroom.border.chat")));
		chat.add(new JScrollPane(textareaMessages));
		chat.add(send);
		SpringUtilities.makeCompactGrid(chat, 2, 1, 0, 0, 0, 0);

		setLayout(new SpringLayout());
		setVisible(true);
		add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, users, chat));
		SpringUtilities.makeCompactGrid(this, 1, 1, 0, 0, 0, 0);
		
	}
	
	public void paint(Graphics g){
		super.paint(g);
		listUsers.setListData(kvnchatroom.getMemberIdsAsArray());
		listUsers.repaint();
	}
	
	public void createJFrame(){
		frame = new JFrame("..:: KVNGroupware Client ::.."+"\t"+"Chatroom: "+kvnchatroom.getId());
		frame.getContentPane().add(this);
		frame.addWindowListener(controller);
		frame.setVisible(true);
		frame.pack();
	}
	
	public void disposeJFrame(){
		frame.dispose();
	}

}
